package UI;
import java.util.Scanner;
import Crt.VareCtr;

/**
 * Write a description of class VareUI here.
 * 
 * @author (Gruppe 5) 
 * @version (a version number or a date)
 */
public class VareUI
{
    private VareCtr vCtr;
    
    /**
     * VareUI Constructor
     *
     */
    public VareUI()
    {
        vCtr = new VareCtr();
    }
    
    /**
     * Method start
     *
     */
    public void start()
    {
        vareMenu();
    }
    
    /**
     * Method vareMenu
     *
     * Methoden Starter den valgte methode du ønsker at bruge
     * ved hjælp af nummer du angiver.
     */
    public void vareMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice = writeVareMenu();
            if (choice == 1)
            {
                createVare();
            }
            else
            {
                if(choice == 2)
                {
                    findVare();
                }
                else
                {
                    if(choice == 3)
                    {
                        listAll();
                    }
                    else
                    {
                    exit = true;
                    }//end else
                }
            }
        }//end while
    }
    
    /**
     * Method writeVareMenu
     *
     * @return The return value
     * 
     * Methoden viser frem hvad du kan vælge på skærmen.
     */
    private int writeVareMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Vare Menu ***");
            System.out.println(" (1) Opret ny Vare");
            System.out.println(" (2) Find vare");
            System.out.println(" (3) Vis alle varer");
            System.out.println(" (4) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    /**
     * Method inputVareID
     *
     * @return The return value
     * 
     * Methoden læser den angivene input du har skrevet
     * og tjekker om du kan tilføje den eller om den er null.
     * 
     * Alle input gør det samme men nogle af som inputOrdreID
     * stopper også bogstaver så du kun kan skrive tal.
     */
    public int inputVareID()
    {   
         boolean ok = false;
        int vID = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv VareID:  ");
           try{
             test = keyboard.nextLine();
             vID = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
           }
        }//end while
        return vID;
    }
    
    public String inputNavn()
    {
         String navn = null;
        boolean ok = false;
        
        while(!ok)
        {
             // makes an object keyboard to have input from the screen
             Scanner keyboard = new Scanner(System.in);  
             System.out.println(" Skriv Navn:  ");
             navn = keyboard.nextLine();
             
             if(!navn.equals(""))
             {
                 ok = true;
             }
             else
             {
                 System.out.println("Du skal skrive et navn -  prøv igen");
             }
        }
         return navn;
    }
    
    public String inputBeskrivelse()
    {
        String beskrivelse = null;
        boolean ok = false;
        
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Adresse:  ");
           beskrivelse = keyboard.nextLine();
           
           if(!beskrivelse.equals(""))
           {
             ok = true;
           }
           else
           {
             System.out.println("Du skal skrive en adresse -  prøv igen");
           }
        }//end while
        return beskrivelse;
    }
    
    public double inputPris()
    {   
        boolean ok = false;
        double pris = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Pris:  ");
           try{
             test = keyboard.nextLine();
             pris = Double.parseDouble(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
           }
        }//end while
        return pris;
    }
    
    public int inputAntal()
    {   
        boolean ok = false;
        int antal = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Antal:  ");
           try{
             test = keyboard.nextLine();
             antal = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }//end while
        return antal;
    }
    
    public int inputMaxLager()
    {   
        boolean ok = false;
        int max = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv Max Lager:  ");
           try{
             test = keyboard.nextLine();
             max = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }//end while
        return max;
    }
    
    public int inputMinLager()
    {   
        boolean ok = false;
        int min = 0;
        String test = null;
        while(!ok)
        {
           // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Skriv min Lager:  ");
           try{
             test = keyboard.nextLine();
             min = Integer.parseInt(test);
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Du skal skrive et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }//end while
        return min;
    }
    
    public String inputLocation()
    {
        String location = null;
        boolean ok = false;
        
        while(!ok)
        {
             // makes an object keyboard to have input from the screen
             Scanner keyboard = new Scanner(System.in);  
             System.out.println(" Skriv location:  ");
             location = keyboard.nextLine();
             
             if(location.matches("[a-åA-Å]+$"))
             {
                 ok = true;
             }
             else
             {
                 System.out.println("Du skal skrive et location -  prøv igen");
             }
        }
        return location;
    }
    
    /**
     * Method exit
     *
     * Methoden retunere dig til den tidliger menu.
     */
    public void exit()
    {
        System.out.println("\n Press Enter to return");
        
        Scanner button = new Scanner(System.in);
        boolean ok = false;
        while(!ok)
        {
            String theButton = button.nextLine();
            ok = true;
        }
    }
    
    /**
     * Method createVare
     *
     * Methoden bruger det angivne id til at
     * oprette en ny Vare og tjekker om der en vare med
     * det samme id.
     */
    private void createVare()
    {
        int id = inputVareID();
        String navn = inputNavn();
        String beskrivelse = inputBeskrivelse();
        double pris = inputPris();
        int antal = inputAntal();
        int maxLager = inputMaxLager();
        int minLager = inputMinLager();
        String location = inputLocation();
        
        vCtr.createVare(id, navn, beskrivelse, pris, antal, maxLager, minLager, location);
    }
    
    /**
     * Method findVare
     *
     * Methoden bruger det angivne id til at finde en vare
     * og udskriver deres info, finder den ikke nogen skriver
     * den at der ikke er nogen med det id.
     */
    private void findVare()
    {
        int vID = inputVareID();
        
        if(vCtr.findVare(vID) != null)
        {
            System.out.println("ID: " + vCtr.getVarer(vID).getID());
            System.out.println("navn: " + vCtr.getVarer(vID).getNavn());
            System.out.println("Beskrivelse: " + vCtr.getVarer(vID).getBeskrivelse());
            System.out.println("Pris: " + vCtr.getVarer(vID).getPris());
            System.out.println("Antal: " + vCtr.getVarer(vID).getAntal());
            System.out.println("Max Lager: " + vCtr.getVarer(vID).getMaxLager());
            System.out.println("Min Lager: " + vCtr.getVarer(vID).getMinLager());
            System.out.println("Location: " + vCtr.getVarer(vID).getLocation());
        }
        else
        {
            System.out.println("Der er ingen kunde med det ID: " + vID);
        }
        
        exit();
    }
    
    /**
     * Method listAll
     * 
     * Methode viser alle Varer der er.
     */
    private void listAll()
    {
        for(int index = 0; index < vCtr.listAll().size(); index++)
        {
            System.out.println("ID: " + vCtr.getVarer(index).getID());
            System.out.println("navn: " + vCtr.getVarer(index).getNavn());
            System.out.println("Beskrivelse: " + vCtr.getVarer(index).getBeskrivelse());
            System.out.println("Pris: " + vCtr.getVarer(index).getPris());
            System.out.println("Antal: " + vCtr.getVarer(index).getAntal());
            System.out.println("Max Lager: " + vCtr.getVarer(index).getMaxLager());
            System.out.println("Min Lager: " + vCtr.getVarer(index).getMinLager());
            System.out.println("Location: " + vCtr.getVarer(index).getLocation());
            System.out.println("");
        }
        
        exit();
    }
}
